package com.demo;

import com.github.xiaoymin.knife4j.core.util.StrUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@SpringBootApplication
public class MybatisPlusApplication {
    private static final String BACKSLASH = "/";
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MybatisPlusApplication.class, args);
        // 打印项目信息
        printlnProjectInfo(context);
    }

    /**
     * 打印项目信息
     *
     * @param context
     */
    private static void printlnProjectInfo(ConfigurableApplicationContext context) {
        try {
            ConfigurableEnvironment environment = context.getEnvironment();
            String serverPort = environment.getProperty("server.port");
            String contextPath = environment.getProperty("server.servlet.context-path");
            contextPath = (contextPath != null && !contextPath.equals("null")) ? contextPath : "";
            if (StrUtil.isNotBlank(contextPath) && !BACKSLASH.equals(contextPath)) {
                contextPath = contextPath + BACKSLASH;
            }
            String localhostDocUrl = "\nhttp://localhost:" + serverPort + contextPath + "/doc.html";
            System.out.println(localhostDocUrl);
            String localhostSwaggerUrl = "http://localhost:" + serverPort + contextPath + "/swagger-ui/index.html";
            System.out.println(localhostSwaggerUrl);
            ClassPathResource resource = new ClassPathResource("/banner/banner_fozubaoyou.txt");
            InputStreamReader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
            String banner = FileCopyUtils.copyToString(reader);
            System.out.println(banner);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
